const pool = require('../utils/utils.js')

module.exports = async function (ctx, next) {
  console.log(ctx.request.body)
  const { url,rankNum, isDelete} = ctx.request.body;
  if (url){
    let sqlStr = ''
    if(rankNum){
        sqlStr = "UPDATE book SET rankNum=" + rankNum + " where url=" + JSON.stringify(url)
    }
    if(isDelete){
        sqlStr = "UPDATE book SET isDelete=" + JSON.stringify(isDelete) + " where url=" + JSON.stringify(url)
    }
    console.log(sqlStr);
    let res = await new Promise((resolve, reject) => {
      pool.query(sqlStr, (err, result) => {
        if (err) {
          reject(err)
        } else {
          resolve(result)
        }
      })
    })
    ctx.body = {
      code: 200,
      msg: '修改成功'
    }
  }else{
    ctx.body = {
      code: 400,
      msg: '修改失败'
    }
  }
  await next()
}